﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerCustomerDao : ICustomerDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="customer">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(Customer customer)
        {
            return new object[]  
            {
                "@CustomerID", customer.CustomerId,
                "@Code", customer.Code,
                "@Description", customer.Description,
                "@Name", customer.Name,
                "@Address", customer.Address,
                "@IsActive", customer.IsActive,
                "@UnitID", customer.UnitId,
                "@CompanyName", customer.CompanyName,
                "@ContactName", customer.ContactName,
                "@Fax", customer.Fax,
                "@PhoneNumber", customer.PhoneNumber
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Customer> Make = reader =>
          new Customer
          {
              CustomerId = reader["CustomerID"].AsId(),
              Code = reader["Code"].AsString(),
              Description = reader["Description"].AsString(),
              Name = reader["Name"].AsString(),
              Address = reader["Address"].AsString(),
              IsActive = reader["IsActive"].AsBool(),
              UnitId = reader["UnitID"].AsInt(),
              CompanyName = reader["CompanyName"].AsString(),
              ContactName = reader["ContactName"].AsString(),
              Fax = reader["Fax"].AsString(),
              PhoneNumber = reader["PhoneNumber"].AsString()
          };

        /// <summary>
        /// Takes the specified supplier identifier.
        /// </summary>
        /// <param name="supplierId">The supplier identifier.</param>
        /// <param name="unitId">The unit identifier.</param>
        /// <returns></returns>
        private static object[] Take(int supplierId, long unitId)
        {
            return new object[]  
            {
                "@CustomerID", supplierId,
                "@UnitID", unitId
            };
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<Customer> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_Customer";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_Customer";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Inserts the customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        public string InsertCustomer(Customer customer)
        {
            const string sql = "uspInsert_Customer";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(customer));
        }

        /// <summary>
        /// Updates the customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        public string UpdateCustomer(Customer customer)
        {
            const string sql = "uspUpdate_Customer";
            return Db.Update(sql, CommandType.StoredProcedure, Take(customer));
        }

        /// <summary>
        /// Deletes the customer.
        /// </summary>
        /// <param name="customer">The customer.</param>
        /// <returns></returns>
        public string DeleteCustomer(Customer customer)
        {
            const string sql = @"uspDelete_Customer";
            object[] parms = { "@CustomerID", customer.CustomerId,
                              "@UnitID", customer.UnitId};
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Gets the customers by unit.
        /// </summary>
        /// <param name="customerId">The unit identifier.</param>
        /// <returns></returns>
        public Customer GetCustomer(int customerId)
        {
            const string sql = @"uspGet_Customer_ByID";
            object[] parms = { "@CustomerID", customerId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }


        public List<Customer> GetCustomerByUnit(int unitId)
        {
            const string sql = @"uspGet_Customer_ByUnitID";
            object[] parms = { "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
    }
}
